
import { reqUserInfo } from '@/api/userInfo'
const state = {
  userName: ''
  // userName:JSON.parse(localStorage.getItem('userInfo'))
}
const mutations = {
  saveUserInfo(state, userName) {
    state.userName = userName
    // localStorage.setItem('userInfo',userName)
  }
}
const actions = {
  async getUserInfo(context) {
    const res = await reqUserInfo()
    if (res.code === 200) {
      context.commit('saveUserInfo', res.data.username)
    } else {
      // 当token过期，会导致获取用户信息失败，需要抛出错误
      return Promise.reject(new Error('获取用户信息失败'))
    }
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
